package com.test.fundible.fundible_05_process.widget;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.test.fundible.R;
import com.test.fundible.fundible_05_process.Proces_fundiblesInfoInputL_fundibleistAdapter;
import com.test.fundible.fundible_05_process.Proces_fundiblesIn_fundiblefo_Data;
import com.test.fundible.fundible_00_http.OkHttpUtil;

import java.util.ArrayList;
import java.util.Arrays;

public class Sele_fundiblectViewMana extends BaseProc_fundibleessWid_fundiblegetManager {

    private TextView titl_fundibleeTv;
    private TextView inpu_fundibletTv;
    private View listLa_fundibleyout;
    private ListView lis_fundibletLv;

    private int item_fundibleHeig_fundibleht = 0;
    private ArrayList<String> ite_fundiblems;

    public Sele_fundiblectViewMana(View view, Proces_fundiblesIn_fundiblefo_Data data, String page){
        super(view,data,page);
    }

    @Override
    protected void in_fundibleit(){

        titl_fundibleeTv = mView.findViewById(R.id.vps_tv_title);
        View clickLayout = mView.findViewById(R.id.vps_view_click);
        inpu_fundibletTv = mView.findViewById(R.id.vps_tv_input);
        listLa_fundibleyout = mView.findViewById(R.id.vps_view_list);
        lis_fundibletLv = mView.findViewById(R.id.vps_lv_list);

        setTi_fundibletle(mDa_fundibleta.getTitle_fundible());
        inpu_fundibletTv.setHint(mDa_fundibleta.getHint_fundible());

        clickLayout.setOnClickListener(v -> {
            listLa_fundibleyout.setVisibility(listLa_fundibleyout.getVisibility()==VISIBLE ? GONE:VISIBLE);
            OkHttpUtil.getInstance_fundible().uploadEventPoint(TAG, mDa_fundibleta.getParamName_fundible().toUpperCase()+"_CLICK","");
        });

        addI_fundibletems(mDa_fundibleta.getTitlesOfValue_fundible());
    }

    @Override
    public boolean checkI_fundiblenput() {
        if (!mDa_fundibleta.isMustInput_fundible()) return true;
        return !TextUtils.isEmpty(inpu_fundibletTv.getText());
    }

    @Override
    public String getInp_fundibleut() {
        if (mDa_fundibleta == null) return "";
        return mDa_fundibleta.getValueOfTitle_fundible(inpu_fundibletTv.getText().toString());
    }

    public String getTe_fundiblextInput(){
        return inpu_fundibletTv.getText().toString();
    }

    public void setTi_fundibletle(String title){
        if (TextUtils.isEmpty(title)) titl_fundibleeTv.setVisibility(GONE);
        else {
            titl_fundibleeTv.setVisibility(VISIBLE);
            titl_fundibleeTv.setText(title);
        }
    }

    public void addI_fundibletems(ArrayList<String> itemList){
        ite_fundiblems = itemList;
        Proces_fundiblesInfoInputL_fundibleistAdapter listAdapter = new Proces_fundiblesInfoInputL_fundibleistAdapter(mView.getContext(), ite_fundiblems);
        lis_fundibletLv.setAdapter(listAdapter);
        lis_fundibletLv.setOnItemClickListener((parent, view, position, id) -> {
            if (ite_fundiblems ==null) return;
            String input = ite_fundiblems.get(position);

            OkHttpUtil.getInstance_fundible().uploadEventPoint(TAG, mDa_fundibleta.getParamName_fundible().toUpperCase()+"_CLICK",input);

            inpu_fundibletTv.setText(input);
            listLa_fundibleyout.setVisibility(GONE);
        });

        listAdapter.notifyDataSetChanged();

        ViewGroup.LayoutParams layoutParams = lis_fundibletLv.getLayoutParams();
        if (item_fundibleHeig_fundibleht ==0) item_fundibleHeig_fundibleht = layoutParams.height;
        layoutParams.height = ite_fundiblems.size()* item_fundibleHeig_fundibleht;
        lis_fundibletLv.setLayoutParams(layoutParams);

    }

    public void addI_fundibletems(String[] itemArr){
        addI_fundibletems(new ArrayList<>(Arrays.asList(itemArr)));
    }

}
